import { Toast } from 'cube-ui'
export default {
    namespaced: true,
    state: {
        cartList: localStorage.getItem('cartList') ? JSON.parse(localStorage.getItem('cartList')) : [],
    },
    mutations: {
        addCourse(state, course) {
            state.course = course
            console.log(course)
            let flag = state.cartList.every((item => {
                return item.id != course.id
            }))
            if (flag) {
                state.cartList.push(course)
                Toast.$create({
                    txt: '添加购物车成功',
                    time: 700,
                    type: 'text'
                }).show()
            } else {
                Toast.$create({
                    txt: '请勿重复添加',
                    time: 700,
                    type: 'text'
                }).show()
            }
            localStorage.setItem('cartList', JSON.stringify(state.cartList))
        },
        //清空购物车
        emptyCarList(state) {
            state.cartList = []
        },
        //删除某个课程
        removeCarList(state, courseId) {
            state.cartList = state.cartList.filter(item => item.id != courseId)
            localStorage.setItem('cartList', JSON.stringify(state.cartList))
            // console.log(state.cartList)
            // console.log(courseId)
        }
    }
}